<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'professions' => [
            '员工' => [
                'income' => 5000,
                'expenses' => 3000,
                'initial_cash' => 10000
            ],
            '自由职业者' => [
                'income' => 7000,
                'expenses' => 4000,
                'initial_cash' => 15000
            ],
            '企业主' => [
                'income' => 10000,
                'expenses' => 5000,
                'initial_cash' => 20000
            ],
            '投资者' => [
                'income' => 8000,
                'expenses' => 3500,
                'initial_cash' => 18000
            ]
        ],
        'selected_profession' => null,
        'cash' => 0,
        'income' => 0,
        'expenses' => 0,
        'assets' => [],
        'liabilities' => []
    ];
}

// 处理职业选择
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['profession'])) {
    $profession = $_POST['profession'];
    $_SESSION['game']['selected_profession'] = $profession;
    $_SESSION['game']['cash'] = $_SESSION['game']['professions'][$profession]['initial_cash'];
    $_SESSION['game']['income'] = $_SESSION['game']['professions'][$profession]['income'];
    $_SESSION['game']['expenses'] = $_SESSION['game']['professions'][$profession]['expenses'];
    $message = "你选择了 {$profession} 职业，初始现金为 {$_SESSION['game']['cash']} 元。";
}

// 处理工作操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['work'])) {
    $_SESSION['game']['cash'] += $_SESSION['game']['income'];
    $_SESSION['game']['cash'] -= $_SESSION['game']['expenses'];
    $message = "你工作了一段时间，获得收入 {$_SESSION['game']['income']} 元，扣除支出 {$_SESSION['game']['expenses']} 元，当前现金为 {$_SESSION['game']['cash']} 元。";
}

// 处理投资操作（简单示例，仅一个投资机会）
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['invest'])) {
    $investment_cost = 5000;
    $investment_income = 1000;
    if ($_SESSION['game']['cash'] >= $investment_cost) {
        $_SESSION['game']['cash'] -= $investment_cost;
        $_SESSION['game']['assets'][] = [
            'name' => '小型投资项目',
            'cost' => $investment_cost,
            'income' => $investment_income
        ];
        $_SESSION['game']['income'] += $investment_income;
        $message = "你投资了小型投资项目，花费 {$investment_cost} 元，获得额外月收入 {$investment_income} 元，当前现金为 {$_SESSION['game']['cash']} 元。";
    } else {
        $message = "现金不足，无法进行投资。";
    }
}

// 检查是否实现财务自由
$total_income = $_SESSION['game']['income'];
$total_expenses = $_SESSION['game']['expenses'];
if ($total_income >= $total_expenses) {
    $is_financially_free = true;
    $message = "恭喜你，实现财务自由！游戏结束。";
    // 重置游戏
    session_destroy();
    session_start();
    $_SESSION['game'] = null;
} else {
    $is_financially_free = false;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>穷爸爸富爸爸现金流网页游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>穷爸爸富爸爸现金流网页游戏</h1>
    <?php if (isset($_SESSION['game'])): ?>
        <?php if ($_SESSION['game']['selected_profession'] === null): ?>
            <form method="post">
                <label for="profession">选择职业：</label>
                <select id="profession" name="profession">
                    <?php foreach ($_SESSION['game']['professions'] as $profession => $data): ?>
                        <option value="<?php echo $profession; ?>">
                            <?php echo $profession;?>（月收入: <?php echo $data['income'];?> 元，月支出: <?php echo $data['expenses'];?> 元，初始现金: <?php echo $data['initial_cash'];?> 元）
                        </option>
                    <?php endforeach; ?>
                </select>
                <button type="submit">确认选择</button>
            </form>
        <?php else: ?>
            <p>当前职业: <?php echo $_SESSION['game']['selected_profession']; ?></p>
            <p>当前现金: <?php echo $_SESSION['game']['cash']; ?> 元</p>
            <p>月收入: <?php echo $_SESSION['game']['income']; ?> 元</p>
            <p>月支出: <?php echo $_SESSION['game']['expenses']; ?> 元</p>
            <p>资产: 
                <?php if (empty($_SESSION['game']['assets'])): ?>
                    暂无
                <?php else: ?>
                    <?php foreach ($_SESSION['game']['assets'] as $asset): ?>
                        <?php echo $asset['name'];?> (成本: <?php echo $asset['cost'];?> 元, 月收入: <?php echo $asset['income'];?> 元), 
                    <?php endforeach; ?>
                <?php endif; ?>
            </p>

            <?php if (isset($message)): ?>
                <div class="message <?php echo strpos($message, '恭喜')!== false? 'success' : 'error'; ?>">
                    <?php echo $message; ?>
                </div>
            <?php endif; ?>

            <form method="post">
                <button type="submit" name="work">去工作</button>
            </form>

            <form method="post">
                <button type="submit" name="invest">投资小型项目（成本 5000 元，月收入 1000 元）</button>
            </form>
        <?php endif; ?>
    <?php else: ?>
        <p>游戏结束，你可以刷新页面重新开始。</p>
    <?php endif; ?>
</body>

</html>